import java.util.*;
import java.io.*;

public class TicketManager {
    private List<Ticket> tickets = new ArrayList<>();
    private static final String TICKET_FILE = "tickets.txt";
    private SessionManager sessionManager;

    public TicketManager(SessionManager sessionManager) {
        this.sessionManager = sessionManager;
        loadTicketsFromFile();
    }

    // 从文件加载票务数据
    private void loadTicketsFromFile() {
        try (BufferedReader br = new BufferedReader(new FileReader(TICKET_FILE))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] parts = line.split(",");
                if (parts.length >= 6) {
                    Session session = sessionManager.getSessionById(parts[1]);
                    if (session != null) {
                        Ticket ticket = new Ticket(parts[0], session,
                                Integer.parseInt(parts[2]),
                                Integer.parseInt(parts[3]), parts[4]);
                        ticket.setRefunded(Boolean.parseBoolean(parts[5]));
                        tickets.add(ticket);
                        // 恢复座位状态
                        if (!ticket.isRefunded()) {
                            session.getSeats()[ticket.getRow()][ticket.getCol()] = true;
                        }
                    }
                }
            }
        } catch (IOException e) {
            System.out.println("票务数据文件不存在，将创建新文件");
        }
    }

    // 保存票务数据到文件
    private void saveTicketsToFile() {
        try (PrintWriter pw = new PrintWriter(new FileWriter(TICKET_FILE))) {
            for (Ticket ticket : tickets) {
                pw.println(ticket.getTicketId() + "," + ticket.getSession().getId() + "," +
                        ticket.getRow() + "," + ticket.getCol() + "," +
                        ticket.getBuyer() + "," + ticket.isRefunded());
            }
        } catch (IOException e) {
            System.out.println("保存票务数据失败：" + e.getMessage());
        }
    }

    public Ticket sellTicket(Session session, int row, int col, String buyer) {
        if (session.getSeats()[row][col]) return null; // 已售
        String ticketId = UUID.randomUUID().toString();
        Ticket ticket = new Ticket(ticketId, session, row, col, buyer);
        tickets.add(ticket);
        session.getSeats()[row][col] = true;
        saveTicketsToFile(); // 保存到文件
        return ticket;
    }

    public boolean refundTicket(String ticketId) {
        for (Ticket t : tickets) {
            if (t.getTicketId().equals(ticketId) && !t.isRefunded()) {
                t.setRefunded(true);
                t.getSession().getSeats()[t.getRow()][t.getCol()] = false;
                saveTicketsToFile(); // 保存到文件
                return true;
            }
        }
        return false;
    }

    public List<Ticket> getTicketsBySession(Session session) {
        List<Ticket> result = new ArrayList<>();
        for (Ticket t : tickets) {
            if (t.getSession().equals(session) && !t.isRefunded()) result.add(t);
        }
        return result;
    }
}